<?php
/**
 * Created by PhpStorm.
 * User: mac
 * Date: 2021/6/11
 * Time: 10:45 PM
 */

namespace Modules\Common\Services;
use Modules\Auth\Helper\GuzzleHttp;
use Illuminate\Support\Facades\Hash;
use Modules\Common\Entities\Model;
use Modules\Common\Contract\Interfaces\MySqlOperation;
use Spatie\Permission\Models\Role;
use Spatie\Permission\Models\Permission;
class CommonService extends BaseService implements MySqlOperation
{
    private $model;
    Private $data;
    private $guzzleHttp;
    private $options;
    /**
     * 设置登陆配置
     */
    public function setGuzzleHttp($config=null){
        $this->guzzleHttp = new GuzzleHttp();
        $this->options = $this->guzzleHttp->getClientConfig($config);
    }
    /**
     * 获取对象
     */
    private function getGuzzleHttp(){
        return $this->guzzleHttp;
    }
    /**
     * 获取登陆配置
     */
    private function getOptions(){
        return $this->options;
    }
    /**
     * 设置传入的数据
     */
    public function set(Model $model,array $data)
    {
        $this->model =  $model;
        $this->data = $data;
    }
    /**
     * 获取模型
     */
    private function getModel(){
        return $this->model;
    }
    /**
     * 获取数据
     */
    private function getData(){
        return $this->data;
    }
    /***
     * 添加数据
     */
    public function store(){
        return $this->getModel()->create($this->getData());
    }
    /**
     * 用户登陆
     */
    public function userLogin(){
        $this->data = $this->getData();
        $user = $this->getModel()->findForPassport($this->data['name']);
        if ( $user ) {
            if (Hash::check(ltrim($this->data['password']), $user->password)) {
              return   $this->getGuzzleHttp()->Login($this->data['name'],$this->data['password'],$this->getOptions());
            }
        }
        return false;
    }
    /**后台登陆
     */
     public function adminLogin(){
        $admin = $this->getModel()->findForPassport($this->getData()['name']);
        if ( $admin ) {
            if (Hash::check(ltrim($this->getData()['password']), $admin->password)) {
              return   $this->getGuzzleHttp()->Login($this->getData()['name'],$this->getData()['password'],$this->getOptions());
            }
        }
        return false;
    }
    /**
    编辑
     */
    public function update(){
      return  $this->getModel()->update($this->getData());
    }

    /**删除
     *
     */
    public function destroy(){
        $this->getModel()->delete();
    }
     /**
     * 权限模型
     */
    public function setPermission($model,array $data)
    {
        $this->model =  $model;
        $this->data = $data;
    }
    /**
     * 创建管理员  选择角色
     */
    public function role(){
        $roles = Role::whereIn('id',$this->getData())->get();
        $this->getModel()->syncRoles($roles);

    }
    /**
     * 创建角色 选择权限
     */
    public function permission(){
        $permissions = Permission::whereIn('id',$this->getData())->get();
        $this->getModel()->syncPermissions($permissions);
    }

}
